package com.lolc.ihm.listener;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JComboBox;

import com.lolc.LoLAutoCounterManager;
import com.lolc.bean.Lane;
import com.lolc.ihm.Constants;
import com.lolc.ihm.component.ComboBoxItem;


public class SaveLaneListener implements ActionListener {

	private String groupName;
	private int id;

	public SaveLaneListener(String groupName, int id)
	{		
		this.groupName=groupName;
		this.id=id;
	}
	
	@Override
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		if(e.getSource() instanceof JComboBox)
		{			
			
			@SuppressWarnings("unchecked")
			JComboBox<ComboBoxItem> comboBox = (JComboBox<ComboBoxItem>) e.getSource();	
			
			Lane selected=null;
			if(comboBox.getSelectedIndex() >= 0)
			{	
				String selectedLaneId=((ComboBoxItem)comboBox.getSelectedItem()).getId();				
				if(this.groupName!=null && LoLAutoCounterManager.getInstance().getLanesMap()!=null && LoLAutoCounterManager.getInstance().getLanesMap().get(selectedLaneId)!=null)
				{
					selected= LoLAutoCounterManager.getInstance().getLanesMap().get(selectedLaneId);
					if(this.groupName.startsWith(Constants.GROUP_MY_TEAM_LANES))
					{
						LoLAutoCounterManager.getInstance().setMyTeamLaneAtPosition(id, selected);
					}
					else if(this.groupName.startsWith(Constants.GROUP_ENNEMY_TEAM_LANES))
					{	
						LoLAutoCounterManager.getInstance().setEnnemyLaneAtPosition(id, selected);
					}
					else if(this.groupName.startsWith(Constants.GROUP_MY_PLAYER_LANE))
					{
						LoLAutoCounterManager.getInstance().setLane(selected);			
					}
				}				
			}
			//Lane is unselected
			else
			{
				if(this.groupName.startsWith(Constants.GROUP_MY_TEAM_LANES))
				{		
					LoLAutoCounterManager.getInstance().setMyTeamLaneAtPosition(id, null);
				}
				else if(this.groupName.startsWith(Constants.GROUP_ENNEMY_TEAM_LANES))
				{						
					LoLAutoCounterManager.getInstance().setEnnemyLaneAtPosition(id, null);
				}
				else if(this.groupName.startsWith(Constants.GROUP_MY_PLAYER_LANE))
				{
					LoLAutoCounterManager.getInstance().setLane(null);
				}
			}
		}
		
		LoLAutoCounterManager.getLogger().debug(LoLAutoCounterManager.getInstance().toString());
	}
}
